﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MalRec.Base;
using MalRec.Base.Service;
using MalRec.Base.ExceptionHandling;
using MalRec.Base.Inteface;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using MyAnimelistRecommender.wAnime.View;
using MyAnimelistRecommender.wAnime.View.Widget;

namespace MyAnimelistRecommender.wAnime
{
    [Module(ModuleName = "wAnimeModule", OnDemand = true)]
    public class wAnimeModule : IModule, IMalRecModule
    {
        #region private members

        /// <summary>
        /// The unity container
        /// </summary>
        private readonly IUnityContainer _container;

        /// <summary>
        /// The region manager
        /// </summary>
        private readonly IRegionManager _regionManager;

        /// <summary>
        /// The module view
        /// </summary>
        private IModuleView _moduleView;

        #endregion private members

        #region public members

        #endregion public members

        #region constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlGeneratorModule"/> class
        /// </summary>
        /// <param name="container">the unity container</param>
        /// <param name="regionManager">the region manager</param>
        public wAnimeModule(IUnityContainer container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;
        }

        #endregion constructors

        #region private methods

        /// <summary>
        /// Registers views and services
        /// </summary>
        private void RegisterViewsAndServices()
        {
            _container.RegisterInstance(typeof(wAnimeModule), this);

            _container.RegisterType<IModulePresenter, ModulePresenter>();
            _container.RegisterType<IModuleView, wAnimeModuleView>();

            //_container.RegisterType<IHtmlGeneratorService, HtmlGeneratorService>(new ContainerControlledLifetimeManager());

            _container.RegisterType<IWidgetPresenter, WidgetPresenter>(new ContainerControlledLifetimeManager());
            _container.RegisterType<IWidgetView, WidgetView>(new ContainerControlledLifetimeManager());

            //_regionManager.RegisterViewWithRegion(Base.RegionName.WidgetRegion, typeof(IWidgetView));
        }

        #endregion private methods

        #region IModule Members

        /// <summary>
        /// Initializes the Newsletter module
        /// </summary>
        public void Initialize()
        {
            try
            {
                RegisterViewsAndServices();
                //_regionManager.RegisterViewWithRegion(Base.RegionName.WidgetRegion, typeof(IWidgetView));
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionHelper.Handle(ex, "Business Logic");
                if (rethrow)
                {
                    throw;
                }
            }
        }

        private static string _ModuleName;

        public static string StaticModuleName
        {
            get
            {
                if (string.IsNullOrEmpty(_ModuleName))
                {
                    var t = typeof(wAnimeModule);
                    foreach (var i in t.GetCustomAttributes(true))
                    {
                        if (i is ModuleAttribute)
                        {
                            _ModuleName = ((ModuleAttribute)i).ModuleName;
                        }
                    }
                }
                return _ModuleName;
            }
        }

        public string ModuleName
        {
            get
            {
                return StaticModuleName;
            }
        }

        #endregion IModule Members

        #region IGigantModule Members

        /// <summary>
        /// Gets or sets the name of the widget
        /// </summary>
        public string WidgetName
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the description of the widget
        /// </summary>
        public string WidgetDescription
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets info if the widget is currently installed
        /// </summary>
        public bool IsWidgetInstalled
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the small widget content
        /// </summary>
        public IView SmallWidgetContent
        {
            get
            {
                IWidgetPresenter presenter = _container.Resolve<IWidgetPresenter>();
                return presenter.View;
            }
        }

        /// <summary>
        /// Gets the large widget content
        /// </summary>
        public IView LargeWidgetContent
        {
            get
            {
                IWidgetPresenter presenter = _container.Resolve<IWidgetPresenter>();
                return presenter.View;
            }
        }

        /// <summary>
        /// Gets the normal widget content
        /// </summary>
        public IView NormalWidgetContent
        {
            get
            {
                IWidgetPresenter presenter = _container.Resolve<IWidgetPresenter>();
                return presenter.View;
            }
        }

        /// <summary>
        /// Shows the module
        /// </summary>
        public object ShowModule(object parameter)
        {
            try
            {
                if (_moduleView == null)
                {
                    _moduleView = _container.Resolve<IModulePresenter>().View;
                }

                if (!_regionManager.Regions[RegionName.ModuleRegion].Views.Contains(_moduleView))
                {
                    _regionManager.RegisterViewWithRegion(RegionName.ModuleRegion, () => _moduleView);
                }
                else
                {
                    _regionManager.Regions[RegionName.ModuleRegion].Activate(_moduleView);
                }
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionHelper.Handle(ex, "Business Logic");
                if (rethrow)
                {
                    throw;
                }
            }
            return null;
        }

        /// <summary>
        /// Closes the module in the shell
        /// </summary>
        public void CloseModule()
        {
            try
            {
                if (_moduleView != null)
                {
                    if (_regionManager.Regions[RegionName.ModuleRegion].Views.Contains(_moduleView))
                    {
                        _regionManager.Regions[RegionName.ModuleRegion].Deactivate(_moduleView);
                    }
                }
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionHelper.Handle(ex, "Business Logic");
                if (rethrow)
                {
                    throw;
                }
            }
        }

        #endregion IGigantModule Members
    }
}